﻿using IceCream.icemodel;
using IceCream;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO.Ports;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System;
/// <summary>
/// IO板生成
/// </summary>
public class IOMsg
{
    private static IOMsg instance;
    private IOMsg() { }

    public static IOMsg Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new IOMsg();
            }
            return instance;
        }
    }
    #region IO通讯
    public bool AllBol = true;
    public bool RobotBol = true;
    public delegate void IOdelegate(string btnName, string tag);
    public IOdelegate iodelegate;
    /// <summary>
    ///  IO的通讯返回数据
    /// </summary>
    private List<byte> buffer = new List<byte>();
    public static bool IsMsgError = true;
    string nostrs = "";
    public Thread All_thr;
    public void IOSerial_Thr()
    {
        try
        {
            var ddd = SerialPort.GetPortNames().ToList();
            if (!SerialPort.GetPortNames().ToList().Any(a => a.ToLower().Contains(IPorPortInfo.Instance.IOPortName)))
            {
                Thread.Sleep(1000);
                IOSerial_Thr();
            }
            IOSerialPort.Instance.IceMachineConnect(IPorPortInfo.Instance.IOPortName, 38400, "", 8, 1);//其他串口通讯
            IOSerialPort.Instance.serialPort.DataReceived += SerialPort_DataReceived;
            MainWindow.Clear();
            Robot_Thr();
            All_thr = new Thread(AllMessages);
            All_thr.IsBackground = true;
            All_thr.Start();
            Thread thread = new Thread(MainWindow.MessageShow);
            thread.IsBackground = true;
            thread.Start();

            //saveDeviceMatter save = new saveDeviceMatter();
            //save.matterStatusJson = JsonConvert.SerializeObject(CoffeeMatters.Instance);
            //save.deviceStatusJson = JsonConvert.SerializeObject(deviceStatus.Instance);
            //var saves = JsonConvert.SerializeObject(save);
            //HttpMsg.Instance.saveDeviceMatterMsg(saves);
        }
        catch (Exception)
        {
            IOSerialPort.Instance.serialPort.DataReceived -= SerialPort_DataReceived;
            Thread.Sleep(150);
            IOSerial_Thr();
        }
    }
    public void Robot_Thr()
    {
        try
        {
            //if (!PingIP(IPorPortInfo.Instance.RobotIPLeft))
            //{
            //    Thread.Sleep(150);
            //    Robot_Thr();
            //}
            // RobotOperate.Read_CSV(); //读取轨迹文件
            RobotOperate.Robot_Initialisation_double(); //机器人初始化
        }
        catch (Exception)
        {
            Thread.Sleep(150);
            Robot_Thr();
        }
    }
    /// <summary>
    /// PING 通 IP
    /// </summary>
    /// <param name="IP"></param>
    /// <returns></returns>
    public bool PingIP(string IP)
    {
        try
        {
            if (IP.Split('.').Count() < 4) return false;
            Ping pingSender = new Ping();
            PingReply reply = pingSender.Send(IP, 120);//第一个参数为ip地址，第二个参数为ping的时间
            if (reply.Status == IPStatus.Success)
            {
                return true;
            }
            else
            {
                //ping不通
                return false;
            }
        }
        catch (Exception)
        {
            return false;
        }
    }
    #region 控制屏

    //public void Msg(string btnName, string tag)
    //{
    //    try
    //    {
    //        IOMsg.Instance.AllBol = false;
    //        Thread.Sleep(150);
    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LOpenBulkPowerDown);
    //        Thread.Sleep(150);
    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LCloseBulkPowerDown);
    //        Thread.Sleep(150);
    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_PTBPE_Down);
    //        Thread.Sleep(150);
    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_XSN_Down);
    //        Thread.Sleep(150);
    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QDCX_Down);
    //        Thread.Sleep(150);
    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_Stop_Down);
    //        Thread.Sleep(150);
    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_ZTCX_Down);
    //        Thread.Sleep(150);
    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JXYXCX_Down);
    //        Thread.Sleep(150);
    //        switch (btnName)
    //        {
    //            case "DO1":
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
    //                Thread.Sleep(150);
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
    //                Thread.Sleep(150);
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_start);
    //                Thread.Sleep(150);
    //                IOMsg.Instance.AllBol = true;
    //                Thread.Sleep(150);
    //                tag = "1";
    //                while (!DiaitalBool.Instance.PushRodUP_Sel_Bol)
    //                {
    //                    Thread.Sleep(150);
    //                }
    //                IOMsg.Instance.AllBol = false;
    //                Thread.Sleep(150);
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
    //                break;
    //            case "DO2":
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
    //                Thread.Sleep(150);
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
    //                Thread.Sleep(150);
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_start);
    //                Thread.Sleep(150);
    //                IOMsg.Instance.AllBol = true;
    //                Thread.Sleep(150);
    //                tag = "1";
    //                while (!DiaitalBool.Instance.PushRodDown_Sel_Bol)
    //                {
    //                    Thread.Sleep(150);
    //                }
    //                IOMsg.Instance.AllBol = false;
    //                Thread.Sleep(150);
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
    //                break;
    //            case "DO3":
    //                if (tag == "0")
    //                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_start);
    //                else
    //                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_end);
    //                break;
    //            case "DO4":
    //                if (tag == "0")
    //                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction2_start);
    //                else
    //                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction2_end);
    //                break;
    //            case "DO5":
    //                if (tag == "0")
    //                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Fructose1_start);
    //                else
    //                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Fructose1_end);
    //                break;
    //            case "DO6":
    //                if (tag == "0")
    //                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Fructose2_start);
    //                else
    //                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Fructose2_end);
    //                break;
    //            case "DO7":
    //                if (tag == "0")
    //                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Fructose3_start);
    //                else
    //                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Fructose3_end);
    //                break;
    //            case "DO8":
    //                IPorPortInfo.Instance.SweetInit1 = Convert.ToInt32(tag);
    //                using (FileStream stream = File.Create(MainWindow.IPorPortUrl))
    //                {
    //                    stream.Close();
    //                    stream.Dispose();
    //                    IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
    //                    File.WriteAllText(MainWindow.IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
    //                }
    //                break;
    //            case "DO9":
    //                IPorPortInfo.Instance.SweetInit2 = Convert.ToInt32(tag);
    //                using (FileStream stream = File.Create(MainWindow.IPorPortUrl))
    //                {
    //                    stream.Close();
    //                    stream.Dispose();
    //                    IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
    //                    File.WriteAllText(MainWindow.IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
    //                }
    //                break;
    //            case "DO10":
    //                IPorPortInfo.Instance.SweetInit3 = Convert.ToInt32(tag);
    //                using (FileStream stream = File.Create(MainWindow.IPorPortUrl))
    //                {
    //                    stream.Close();
    //                    stream.Dispose();
    //                    IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
    //                    File.WriteAllText(MainWindow.IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
    //                }
    //                break;
    //            case "DO11":
    //                Thread.Sleep(150);
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn1_start);
    //                DigitalMsgBol.Instance.RobotPowerOn1_Bol = true;
    //                Thread.Sleep(150);
    //                while (true)
    //                {
    //                    RobotOperate.CX_kj();
    //                    if (RobotOperate.state.poweredOn == 1)
    //                    {
    //                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn1_end);
    //                        Thread.Sleep(150);
    //                        break;
    //                    }
    //                    Thread.Sleep(10);
    //                }
    //                RobotMsg(true, false, false, false, false, false, false);
    //                break;
    //            case "DO12":
    //                Thread.Sleep(150);
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_BJ);
    //                Thread.Sleep(150);
    //                RobotMsg(false, false, false, false, false, false, true);
    //                break;
    //            case "DO13":
    //                Thread.Sleep(150);
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LOpenBulkPowerUP);
    //                Thread.Sleep(150);
    //                while (true)
    //                {
    //                    RobotOperate.CX_status();
    //                    if (RobotOperate.status.powered_on == 1)
    //                    {
    //                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LOpenBulkPowerDown);
    //                        Thread.Sleep(150);
    //                        break;
    //                    }
    //                    Thread.Sleep(10);
    //                }
    //                RobotMsg(false, true, false, false, false, false, false);
    //                break;
    //            case "DO14":
    //                Thread.Sleep(150);
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LCloseBulkPowerUP);
    //                Thread.Sleep(150);
    //                RobotBol = true;
    //                while (true)
    //                {
    //                    RobotOperate.CX_status();
    //                    if (RobotOperate.status.powered_on == 0)
    //                    {
    //                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LCloseBulkPowerDown);
    //                        Thread.Sleep(150);
    //                        break;
    //                    }
    //                    Thread.Sleep(10);
    //                }
    //                RobotMsg(true, false, false, false, false, false, false);
    //                break;
    //            case "DO15":
    //                Thread.Sleep(150);
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_PTBPE_UP);
    //                Thread.Sleep(150);
    //                RobotBol = false;
    //                while (true)
    //                {
    //                    RobotOperate.CX_status();
    //                    if (RobotOperate.status.enabled == 1)
    //                    {
    //                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_PTBPE_Down);
    //                        Thread.Sleep(150);
    //                        break;
    //                    }
    //                    Thread.Sleep(10);
    //                }
    //                RobotMsg(false, false, true, false, false, false, false);
    //                break;
    //            case "DO16":
    //                Thread.Sleep(150);
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_XSN_UP);
    //                Thread.Sleep(150);
    //                RobotBol = true;
    //                while (true)
    //                {
    //                    RobotOperate.CX_status();
    //                    if (RobotOperate.status.enabled == 0)
    //                    {
    //                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_XSN_Down);
    //                        Thread.Sleep(150);
    //                        break;
    //                    }
    //                    Thread.Sleep(10);
    //                }
    //                RobotMsg(false, true, false, false, false, false, false);
    //                break;
    //            case "DO17":
    //                Thread.Sleep(150);
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QDCX_UP);
    //                Thread.Sleep(150);
    //                while (true)
    //                {
    //                    RobotOperate.CX_Start();
    //                    if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
    //                    {
    //                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QDCX_Down);
    //                        Thread.Sleep(150);
    //                        break;
    //                    }
    //                    Thread.Sleep(10);
    //                }
    //                RobotMsg(false, false, false, true, false, false, false);
    //                break;
    //            case "DO18":
    //                Thread.Sleep(150);
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_Stop_UP);
    //                Thread.Sleep(150);
    //                while (true)
    //                {
    //                    RobotOperate.CX_Start();
    //                    if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_IDLE)
    //                    {
    //                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_Stop_Down);
    //                        Thread.Sleep(150);
    //                        break;
    //                    }
    //                    Thread.Sleep(10);
    //                }
    //                RobotMsg(false, false, false, false, true, false, false);
    //                break;
    //            case "DO19":
    //                Thread.Sleep(150);
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_ZTCX_UP);
    //                DigitalMsgBol.Instance.RobotPowerOn1_Bol = true;
    //                Thread.Sleep(150);
    //                RobotBol = false;
    //                while (true)
    //                {
    //                    RobotOperate.CX_Start();
    //                    if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_PAUSED)
    //                    {
    //                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_ZTCX_Down);
    //                        Thread.Sleep(150);
    //                        break;
    //                    }
    //                    Thread.Sleep(10);
    //                }
    //                RobotMsg(false, false, false, false, false, true, false);
    //                break;
    //            case "DO20":
    //                Thread.Sleep(150);
    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JXYXCX_UP);
    //                DigitalMsgBol.Instance.RobotPowerOn1_Bol = true;
    //                Thread.Sleep(150);
    //                RobotBol = false;
    //                while (true)
    //                {
    //                    RobotOperate.CX_Start();
    //                    if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
    //                    {
    //                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JXYXCX_Down);
    //                        Thread.Sleep(150);
    //                        break;
    //                    }
    //                    Thread.Sleep(10);
    //                }
    //                RobotMsg(false, false, false, true, false, false, false);
    //                break;
    //            default:
    //                break;
    //        }
    //        Console.WriteLine(btnName + " " + tag);
    //        iodelegate(btnName, tag == "1" ? "0" : "1");
    //        Thread.Sleep(150);
    //        IOMsg.Instance.AllBol = true;
    //    }
    //    catch (Exception)
    //    {
    //        return;
    //    }
    //}
    ///// <summary>
    ///// 机器人状态
    ///// </summary>
    ///// <param name="KJ">开机</param>
    ///// <param name="SD">上电</param>
    ///// <param name="SN">使能</param>
    ///// <param name="YX">运行</param>
    ///// <param name="TZ">停止</param>
    ///// <param name="ZT">暂停</param>
    ///// <param name="BJ">报警</param>
    //public void RobotMsg(bool KJ, bool SD, bool SN, bool YX, bool TZ, bool ZT, bool BJ)
    //{
    //    Robot_Bol.Instance.Robot_YKJ = KJ;
    //    Robot_Bol.Instance.Robot_SD = SD;
    //    Robot_Bol.Instance.Robot_SN = SN;
    //    Robot_Bol.Instance.Robot_YX = YX;
    //    Robot_Bol.Instance.Robot_TZ = TZ;
    //    Robot_Bol.Instance.Robot_ZT = ZT;
    //    Robot_Bol.Instance.Robot_BJ = BJ;
    //}
    #endregion
    public void AllMessages()
    {
        try
        {
            while (true)
            {
                // Console.WriteLine(AllBol + "chaxun");
                if (AllBol)
                {
                    Thread.Sleep(100);
                    IOSerialPort.Instance.SerialSend(IceCream.icemodel.DigitalMsg.Instance.AllMessage);
                    /// Console.WriteLine("chaxun "+DateTime.Now.ToString());
                }
                else
                    Thread.Sleep(100);
            }
        }
        catch (Exception)
        {
            return;
        }
    }
    private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        try
        {
            byte[] result = new byte[IOSerialPort.Instance.serialPort.BytesToRead];
            IOSerialPort.Instance.serialPort.Read(result, 0, IOSerialPort.Instance.serialPort.BytesToRead);
            buffer.AddRange(result);
            string str = "";// $"{DateTime.Now}：\n";
                            //判断读写模式
            if (buffer.Count > 0x03)
                str += $"{Encoding.ASCII.GetString(result)}";
            for (int i = 0; i < result.Length; i++)
            {
                str += $"{result[i].ToString("X2")} ";
            }
            MsgShow(str);
            Thread.Sleep(1);
        }
        catch (Exception)
        {
            return;
        }
    }
    bool IsError = false;
    bool IsYBJ = false;
    bool Iswl = false;
    public int IsNC = 0;
    public int IsBQL = 0;
    public void DigitalMsgBol2(string str)
    {
        try
        {
            var Bol = str.Split(' ');
            bool bol = false;

            if (MainWindow.IsCSMakeOR)
            {
                if (!(Bol[4] == "01" ? true : false))
                {
                    if (IsBQL >= 10)
                    {
                        MainWindow.IsCSMakeOR = false;
                        IsBQL = 0;
                        Console.WriteLine("nazoyl");
                    }
                    else
                    {
                        Console.WriteLine("nazoyl  " + IsBQL);
                        IsBQL++;
                    }
                }
                else
                {
                    IsBQL = 0;
                }
            }
            else
            {
                MainWindow.IsCupICE = Bol[4] == "01" ? true : false;
            }
            if (DiaitalBool.Instance.BQLCCJC_Bol != (Bol[4] == "01" ? true : false))
            {
                WuLiaoIceSel.Instance.IsIceChuCan = Bol[4] == "01" ? true : false;
                bol = true;
            }
            DiaitalBool.Instance.BQLCCJC_Bol = Bol[4] == "01" ? true : false;
            var ds = Bol[8] == "01" ? true : false;

            if (MainWindow.IsMilkMakeOR)
            {
                if (!(Bol[12] == "01" ? true : false))
                {
                    if (IsNC >= 10)
                    {
                        MainWindow.IsMilkMakeOR = false;
                        IsNC = 0;
                        Console.WriteLine("nazoyl");
                    }
                    else
                    {
                        Console.WriteLine("nazoyl  " + IsNC);
                        IsNC++;
                    }
                }
                else
                {
                    IsNC = 0;
                }
            }
            else
            {
                MainWindow.IsCupNC = Bol[12] == "01" ? true : false;
                Console.WriteLine("ceshiceshi " + DateTime.Now.ToString() + " " + MainWindow.IsCupNC);
            }
            if (DiaitalBool.Instance.BQLCCJC_Bol != (Bol[4] == "01" ? true : false))
            {
                WuLiaoIceSel.Instance.IsIceChuCan = Bol[4] == "01" ? true : false;
                bol = true;
            }
            DiaitalBool.Instance.BQLCCJC_Bol = Bol[4] == "01" ? true : false;
            Console.WriteLine("guanshan " + ds + " " + DateTime.Now.ToString());
            if (DiaitalBool.Instance.NCCCJC_Bol != (Bol[12] == "01" ? true : false))
            {
                WuLiaoIceSel.Instance.IsMilkChuCan = Bol[12] == "01" ? true : false;
                bol = true;
            }
            DiaitalBool.Instance.NCCCJC_Bol = Bol[12] == "01" ? true : false;
            if (DiaitalBool.Instance.NC1_Bol != (Bol[14] == "01" ? true : false))
            {
                WuLiaoIceSel.Instance.IsMilk1Material = Bol[14] == "01" ? true : false;
                bol = true;
            }
            DiaitalBool.Instance.NC1_Bol = Bol[14] == "01" ? true : false;
            Console.WriteLine("奶茶1 " + DiaitalBool.Instance.NC1_Bol);
            if (DiaitalBool.Instance.NC2_Bol != (Bol[16] == "01" ? true : false))
            {
                WuLiaoIceSel.Instance.IsMilk2Material = Bol[16] == "01" ? true : false;
                bol = true;
            }
            DiaitalBool.Instance.NC2_Bol = Bol[16] == "01" ? true : false;
            Console.WriteLine("奶茶2 " + DiaitalBool.Instance.NC2_Bol);
            if (DiaitalBool.Instance.NC3_Bol != (Bol[18] == "01" ? true : false))
            {
                WuLiaoIceSel.Instance.IsMilk3Material = Bol[18] == "01" ? true : false;
                bol = true;
            }
            DiaitalBool.Instance.NC3_Bol = Bol[18] == "01" ? true : false;
            Console.WriteLine("奶茶3 " + DiaitalBool.Instance.NC3_Bol);
            if (DiaitalBool.Instance.NC4_Bol != (Bol[20] == "01" ? true : false))
            {
                WuLiaoIceSel.Instance.IsMilk4Material = Bol[20] == "01" ? true : false;
                bol = true;
            }
            DiaitalBool.Instance.NC4_Bol = Bol[20] == "01" ? true : false;
            Console.WriteLine("奶茶4 " + DiaitalBool.Instance.NC4_Bol);
            if (DiaitalBool.Instance.NC5_Bol != (Bol[22] == "01" ? true : false))
            {
                WuLiaoIceSel.Instance.IsMilk5Material = Bol[22] == "01" ? true : false;
                bol = true;
            }
            DiaitalBool.Instance.NC5_Bol = Bol[22] == "01" ? true : false;
            Console.WriteLine("奶茶5 " + DiaitalBool.Instance.NC5_Bol);
            if (DiaitalBool.Instance.NC6_Bol != (Bol[24] == "01" ? true : false))
            {
                WuLiaoIceSel.Instance.IsMilk6Material = Bol[24] == "01" ? true : false;
                bol = true;
            }
            DiaitalBool.Instance.NC6_Bol = Bol[24] == "01" ? true : false;
            Console.WriteLine("奶茶6 " + DiaitalBool.Instance.NC6_Bol);
            //if (DiaitalBool.Instance.Water_Sel_Bol != (Bol[26] == "01" ? true : false))
            //{
            //    if (DiaitalBool.Instance.Water_Sel_Bol)
            //        IsError = true;
            //    CoffeeMatters.Instance.CoffeMatter2 = Bol[26] == "01" ? 1 : 0;
            //    bol = true;
            //    Iswl = true;
            //}
            //DiaitalBool.Instance.Water_Sel_Bol = Bol[34] == "01" ? true : false;
            Console.WriteLine("水 " + DiaitalBool.Instance.Water_Sel_Bol);
            if (bol)
            {
                IceCream.ClientList clientList = new IceCream.ClientList();
                clientList.MsgIceBol = WuLiaoIceSel.Instance;
                clientList.code = 1;
                SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
            }
        }
        catch (Exception)
        {
            return;
        }
    }
    public void MsgShow(string str)
    {
        try
        {
            // Console.WriteLine(str);
            int count = str.Length / 315;
            if (count > 0)
            {
                var str1 = str.Substring(str.Length - count * 315, 315);
                //Console.WriteLine(str1);
                if (str1.Split(' ').Count() == 105)
                {
                    DigitalMsgBol2(str1);
                    if (count > 1)
                        MsgShow(str.Substring(str.Length - (count - 1) * 315, (count - 1) * 315));
                }
                else
                {
                    var nostr = str1.Split(' ');
                    int cc = int.TryParse(nostr[0].Substring(nostr[0].Length - 2, 2), out int a) ? a : -1;
                    if (cc != -1)
                    {
                        nostrs += nostr[0].Substring(nostr[0].Length - 2, 2) + " ";
                        for (int i = 1; i < nostr.Count() - 1; i++)
                        {
                            nostrs += nostr[i] + " ";
                        }
                        // Console.WriteLine(nostrs);
                        if (nostrs.Substring(0, 5) == "02 04")
                        {
                            if (nostrs.Count() >= 315)
                            {
                                DigitalMsgBol2(nostrs);
                                nostrs = "";
                            }
                        }
                        else
                        {
                            nostrs = "";
                        }
                    }
                }
            }
            else
            {

                var nostr = str.Split(' ');
                nostrs += nostr[0].Substring(nostr[0].Length - 2, 2) + " ";
                for (int i = 1; i < nostr.Count() - 1; i++)
                {
                    nostrs += nostr[i] + " ";
                }
                // Console.WriteLine(nostrs);
                if (nostrs.Substring(0, 5) == "02 04")
                {
                    if (nostrs.Count() >= 315)
                    {
                        DigitalMsgBol2(nostrs);
                        nostrs = "";
                    }
                }
                else
                {
                    if (nostrs.Substring(0, 5) == "01 05")
                    {
                        Console.WriteLine(str);
                    }
                    else if (nostrs.Substring(0, 5) == "03 02")
                    {
                        Console.WriteLine(nostrs);
                        var robstr = nostrs.Split(' ');
                        RobotBol = robstr[3] == "01" ? true : false;
                        Console.WriteLine(robstr[3] == "01" ? true : false);
                    }
                    else if (nostrs.Substring(0, 5) == "04 02")
                    {
                        Console.WriteLine(nostrs);
                        var robstr = nostrs.Split(' ');
                        RobotBol = robstr[3] == "01" ? true : false;
                        Console.WriteLine(robstr[3] == "01" ? true : false);
                    }
                    nostrs = "";
                }
            }
        }
        catch (Exception)
        {
            return;
        }
    }
    #endregion
}